package com.zbvc.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zbvc.dao.BookDao;
import com.zbvc.dao.impl.BookDaoImpl;
import com.zbvc.entity.Book;

/**
 * Servlet implementation class FindBookServlet
 */
@WebServlet(name = "findBookByName", urlPatterns = { "/findBookByName" })
public class FindBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindBookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 处理中文乱码
		// 防止中文乱码
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		String tip = request.getParameter("tip");
		String name = request.getParameter("name");
		
		BookDao bookDao = new BookDaoImpl();
		List<Book> books = bookDao.findBookByName(name);
		// 将查询到的book数据绑定到session对象中，然后可以在admin_book.jsp取出
		HttpSession session = request.getSession();
		session.setAttribute("bookdata", books);
		// tip =1表示管理员在搜索 tip!=1 表示读者
		if("1".equals(tip)) {
			// 跳转到admin_book.jsp
			response.sendRedirect("/bookManager/admin/admin_book.jsp");
		}else {
			// 读者在搜索
			response.sendRedirect("/bookManager/user/user_select.jsp");
			
		}
		
		
		
		//500错误 java  NullPoint   NumformatExeption   ClassNotFound   SQLExcetipion  sql语法写错
		
	}

}
